﻿Public Class SettingsEntry
  Implements MobilePrettyGui.iPrettyListItem
  Implements IDisposable

  ' State info
  Private _clickRect As Rectangle = Rectangle.Empty
  Private _isSelected As Boolean = False

  ' Settings entry
  Private _text As String = ""            ' Text to show
  Private _subtext As String = ""         ' Subtext to show on line 2
  Private _key As String = ""             ' Key that corresponds to the menu item
  Private _schema As New SchemaSettings
  Private _stringValue As String = String.Empty
  Private _intValue As Integer = 0

  ' Shared amongst all same entities
  Private Shared slc As MobilePrettyGui.PrettyList.ListColors
  Private Shared sbrBg As Brush = Nothing  ' Background normal
  Private Shared sbrBgA As Brush = Nothing ' Background alternate
  Private Shared sbrBgS As Brush = Nothing ' Background selected
  Private Shared sbrFg As Brush = Nothing  ' Foreground normal
  Private Shared sbrFg2 As Brush = Nothing ' Foreground line 2
  Private Shared sbrHL As Brush = Nothing  ' Foreground match highlight
  Private Shared sbrFgS As Brush = Nothing ' Foreground selected

  ' Variables used for drawing
  Private _rf As New Drawing.Font("Tahoma", 9, Drawing.FontStyle.Regular)
  Private _bf As New Drawing.Font("Tahoma", 10, Drawing.FontStyle.Bold)
  Private _height As Integer = -1      ' Cached height (for better performance drawing)

  Public Class SchemaSettings
    Private _restartRequired As Boolean = False
    Private _schemaType As SchemaTypeEnum = SchemaTypeEnum.BOOL
    Private _intMinimum As Integer = Integer.MinValue
    Private _intMaximum As Integer = Integer.MaxValue
    Private _intStep As Integer = 1
    Private _radioButtons As New List(Of RadioEntry)
    Private _section As String = ""         ' The key used to specify the section
    Private _key As String = ""             ' The key used to specify the setting

    Public Enum SchemaTypeEnum
      BOOL = 0
      INT
      FILE
      FOLDERS
      LANGUAGE
      SKIN
      RADIO
      STRING_LIST
      REGEX_PATH
      CONTROL_EDITOR
      STREAMREGEX_EDITOR
      SUBSCRIPTION_EDITOR
    End Enum

    Public Class RadioEntry
      Private _value As String = String.Empty
      Private _langString As String = String.Empty

      Public Overrides Function ToString() As String
        Return Me._langString
      End Function

      Public Property Value() As String
        Get
          Return Me._value
        End Get
        Set(ByVal value As String)
          Me._value = value
        End Set
      End Property

      Public Property LanguageString() As String
        Get
          Return Me._langString
        End Get
        Set(ByVal value As String)
          Me._langString = value
        End Set
      End Property
    End Class

    Public Property RestartRequired() As Boolean
      Get
        Return Me._restartRequired
      End Get
      Set(ByVal value As Boolean)
        Me._restartRequired = value
      End Set
    End Property

    Public Property SchemaType() As SchemaTypeEnum
      Get
        Return Me._schemaType
      End Get
      Set(ByVal value As SchemaTypeEnum)
        Me._schemaType = value
      End Set
    End Property

    Public Property IntMinimum() As Integer
      Get
        Return Me._intMinimum
      End Get
      Set(ByVal value As Integer)
        Me._intMinimum = value
      End Set
    End Property

    Public Property IntMaximum() As Integer
      Get
        Return Me._intMaximum
      End Get
      Set(ByVal value As Integer)
        Me._intMaximum = value
      End Set
    End Property

    Public Property IntStep() As Integer
      Get
        Return Me._intStep
      End Get
      Set(ByVal value As Integer)
        Me._intStep = value
      End Set
    End Property

    Public ReadOnly Property RadioButtons() As List(Of RadioEntry)
      Get
        Return Me._radioButtons
      End Get
    End Property

    Public Property SectionKey() As String
      Get
        Return Me._section
      End Get
      Set(ByVal value As String)
        Me._section = value
      End Set
    End Property

    Public Property SettingsKey() As String
      Get
        Return Me._key
      End Get
      Set(ByVal value As String)
        Me._key = value
      End Set
    End Property
  End Class

  Public Property StringValue() As String
    Get
      Return Me._stringValue
    End Get
    Set(ByVal value As String)
      Me._stringValue = value
    End Set
  End Property

  Public Property IntValue() As Integer
    Get
      Return Me._intValue
    End Get
    Set(ByVal value As Integer)
      Me._intValue = value
    End Set
  End Property

  Public ReadOnly Property Schema() As SchemaSettings
    Get
      Return Me._schema
    End Get
  End Property

  Public Property Text() As String
    Get
      Return Me._text
    End Get
    Set(ByVal value As String)
      Me._text = value
      Me._height = -1
    End Set
  End Property

  Public Property Subtext() As String
    Get
      Return Me._subtext
    End Get
    Set(ByVal value As String)
      Me._subtext = value
      Me._height = -1
    End Set
  End Property

  Public Property Key() As String
    Get
      Return Me._key
    End Get
    Set(ByVal value As String)
      Me._key = value
    End Set
  End Property

  Public Shared Sub SetColors(ByVal lc As MobilePrettyGui.PrettyList.ListColors)
    If sbrBg Is Nothing OrElse slc.BackgroundColor <> lc.BackgroundColor Then
      sbrBg = New SolidBrush(lc.BackgroundColor)
    End If
    If sbrBgA Is Nothing OrElse slc.BackgroundColorAlternate <> lc.BackgroundColorAlternate Then
      sbrBgA = New SolidBrush(lc.BackgroundColorAlternate)
    End If
    If sbrBgS Is Nothing OrElse slc.BackgroundColorSelected <> lc.BackgroundColorSelected Then
      sbrBgS = New SolidBrush(lc.BackgroundColorSelected)
    End If
    If sbrBgS Is Nothing OrElse slc.BackgroundColorSelected <> lc.BackgroundColorSelected Then
      sbrBgS = New SolidBrush(lc.BackgroundColorSelected)
    End If
    If sbrFg Is Nothing OrElse slc.TextColor1 <> lc.TextColor1 Then
      sbrFg = New SolidBrush(lc.TextColor1)
    End If
    If sbrFg2 Is Nothing OrElse slc.TextColor2 <> lc.TextColor2 Then
      sbrFg2 = New SolidBrush(lc.TextColor2)
    End If
    If sbrHL Is Nothing OrElse slc.TextColor3 <> lc.TextColor3 Then
      sbrHL = New SolidBrush(lc.TextColor3)
    End If
    If sbrFgS Is Nothing OrElse slc.TextcolorSelected <> lc.TextcolorSelected Then
      sbrFgS = New SolidBrush(lc.TextcolorSelected)
    End If

    slc = lc
  End Sub

  Public Property ClickRectangle() As System.Drawing.Rectangle Implements MobilePrettyGui.iPrettyListItem.ClickRectangle
    Get
      Return Me._clickRect
    End Get
    Set(ByVal value As System.Drawing.Rectangle)
      Me._clickRect = value
    End Set
  End Property

  Public Sub DrawPrettyListItem(ByVal g As System.Drawing.Graphics, ByVal dpiFactor As Single, ByVal colors As MobilePrettyGui.PrettyList.ListColors, ByVal rect As System.Drawing.Rectangle, ByVal isAlternate As Boolean) Implements MobilePrettyGui.iPrettyListItem.DrawPrettyListItem
    ' Check shared colors
    SetColors(colors)

    Dim brBg As Brush
    Dim brBgA As Brush
    Dim brFg As Brush
    Dim brFg2 As Brush
    Dim brHL As Brush

    If Me._isSelected Then
      brBg = sbrBgS
      brBgA = sbrBgS
      brFg = sbrFgS
      brFg2 = sbrFgS
      brHL = sbrFgS
    Else
      brBg = sbrBg
      brBgA = sbrBgA
      brFg = sbrFg
      brFg2 = sbrFg2
      brHL = sbrHL
    End If

    Dim sfText As Drawing.SizeF = g.MeasureString(Me._text, Me._bf)
    Dim sfSubtext As Drawing.SizeF = g.MeasureString(Me._subtext, Me._rf)
    Dim y As Integer = (rect.Height - (sfText.Height + sfSubtext.Height + (2 * dpiFactor))) / 2

    g.FillRectangle(IIf(isAlternate, brBgA, brBg), rect)

    If Playlists.AVPLPlaylistEntry.SearchTerm.Length > 0 Then
      Playlists.AVPLPlaylistEntry.DrawHighlight(Me._text, Me._bf, brFg, brHL, g, rect.X, rect.Y + y)
      Playlists.AVPLPlaylistEntry.DrawHighlight(Me._subtext, Me._rf, brFg2, brHL, g, rect.X, rect.Y + y + (sfText.Height) + (2 * dpiFactor))
    Else
      g.DrawString(Me._text, Me._bf, brFg, rect.X, rect.Y + y)
      g.DrawString(Me._subtext, Me._rf, brFg2, rect.X, rect.Y + y + (sfText.Height) + (2 * dpiFactor))
    End If
  End Sub

  Public Function DrawPrettyListItemHeight(ByVal g As System.Drawing.Graphics, ByVal dpiFactor As Single) As Integer Implements MobilePrettyGui.iPrettyListItem.DrawPrettyListItemHeight
    If Me._height < 0 Then
      Dim sfText As Drawing.SizeF = g.MeasureString("Pp", Me._bf)
      Dim sfSubtext As Drawing.SizeF = g.MeasureString("Pp", Me._rf)
      Me._height = sfText.Height + sfSubtext.Height + (6 * dpiFactor)
    End If

    Return Me._height
  End Function

  Public ReadOnly Property FilterIsIncluded(ByVal filter As String) As Boolean Implements MobilePrettyGui.iPrettyListItem.FilterIsIncluded
    Get
      If Me._text.ToUpper.Contains(filter) Then Return True
      If Me._subtext.ToUpper.Contains(filter) Then Return True
      Return False
    End Get
  End Property

  Public Property IsSelected() As Boolean Implements MobilePrettyGui.iPrettyListItem.IsSelected
    Get
      Return Me._isSelected
    End Get
    Set(ByVal value As Boolean)
      Me._isSelected = value
    End Set
  End Property

  Public Sub OnMouseClick() Implements MobilePrettyGui.iPrettyListItem.OnMouseClick
    Me._isSelected = False
  End Sub

  Public Sub OnMouseLongClick() Implements MobilePrettyGui.iPrettyListItem.OnMouseLongClick
    Me._isSelected = False
  End Sub

  Public Sub OnMouseDown() Implements MobilePrettyGui.iPrettyListItem.OnMouseDown
    Me._isSelected = True
  End Sub

  Public Sub OnMouseScroll() Implements MobilePrettyGui.iPrettyListItem.OnMouseScroll
    Me._isSelected = False
  End Sub

  Public Sub OnMouseUp() Implements MobilePrettyGui.iPrettyListItem.OnMouseUp
    Me._isSelected = False
  End Sub


#Region " IDisposable Support "
  Private disposedValue As Boolean = False    ' To detect redundant calls

  ' IDisposable
  Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    If Not Me.disposedValue Then
      If disposing Then
        ' TODO: free other state (managed objects).
      End If
    End If
    Me.disposedValue = True
  End Sub

  ' This code added by Visual Basic to correctly implement the disposable pattern.
  Public Sub Dispose() Implements IDisposable.Dispose
    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    Dispose(True)
    GC.SuppressFinalize(Me)
  End Sub
#End Region

End Class
